/*
 * Copyright 2022-2023 Advanced Micro Devices Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <fstream>
#include <iostream>
#include <memory>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <vitis/ai/bevdet.hpp>
#include <vitis/ai/env_config.hpp>

#include "process_result.hpp"
using namespace std;
using namespace cv;

void LoadImageNames(std::string const& filename,
                    std::vector<std::string>& images) {
  images.clear();

  /*Check if path is a valid directory path. */
  FILE* fp = fopen(filename.c_str(), "r");
  if (NULL == fp) {
    fprintf(stdout, "open file: %s  error\n", filename.c_str());
    exit(1);
  }

  char buffer[256] = {0};
  while (fgets(buffer, 256, fp) != NULL) {
    int n = strlen(buffer);
    buffer[n - 1] = '\0';
    std::string name = buffer;
    images.push_back(name);
  }

  fclose(fp);
}

int main(int argc, char* argv[]) {
  if (argc < 3) {
    std::cerr << "usage : " << argv[0]
              << "<model_name0> <model_name1> <model_name2> <image_path_file> "
                 "<bin_path_file> "
              << std::endl;
    abort();
  }
  auto runner = vitis::ai::BEVdet::create(argv[1], argv[2], argv[3]);
  if (!runner) { // supress coverity complain
      std::cerr <<"create error\n";
      abort();
  }  

  std::vector<std::string> names;
  LoadImageNames(argv[4], names);
  std::vector<cv::Mat> images;
  for (auto&& i : names) {
    images.emplace_back(cv::imread(i));
  }
  std::vector<std::vector<char>> bins;
  std::vector<std::string> bin_names;
  LoadImageNames(argv[5], bin_names);
  for (auto&& i : bin_names) {
    auto infile = std::ifstream(i, std::ios_base::binary);
    bins.emplace_back(std::vector<char>(std::istreambuf_iterator<char>(infile),
                                        std::istreambuf_iterator<char>()));
  }
  auto res = runner->run(images, bins);
  for (size_t i = 0; i < 32 && i < res.size(); i++) {
    const auto& r = res[i];
    cout << "label: " << r.label << " score: " << r.score
         << " bbox: " << r.bbox[0] << " " << r.bbox[1] << " " << r.bbox[2]
         << " " << r.bbox[3] << " " << r.bbox[4] << " " << r.bbox[5] << " "
         << r.bbox[6] << " " << r.bbox[7] << " " << r.bbox[8] << endl;
  }
  if (res.size()) {
    auto img = draw_bev(res);
    cv::imwrite("bev_out.jpg", img);
  }

  return 0;
}

// clang-format off
/*
[-4.3250e+00, -1.0612e+01, -2.2005e+00,  6.6614e-01,  1.9887e+00,   1.1510e+00, -2.9349e+00,  0.0000e+00,  0.0000e+00],0.6225,7
[-6.1875e-01, -2.4375e+01, -2.6101e+00,  2.0842e+00,  4.9222e+00,   1.7827e+00, -3.0955e+00,  6.2500e-01,  8.1250e+00],0.6225,0
[-1.4269e+01,  3.7913e+01, -1.0089e+00,  2.6346e+00,  8.1153e+00,   3.0802e+00,  2.7539e+00,  0.0000e+00,  0.0000e+00],0.2689,3
[ 3.0862e+01, -3.0000e+01, -2.4366e+00,  7.2027e-01,  7.5484e-01,   1.8108e+00,  2.2173e+00, -6.5625e-01,  7.3438e-01],0.2227,8
[ 3.1600e+01, -3.0763e+01, -2.4366e+00,  7.0911e-01,  7.5484e-01,   1.8108e+00,  2.2052e+00, -6.5625e-01,  7.1875e-01],0.2227,8
[-9.9250e+00,  2.9200e+01, -1.4035e+00,  4.6504e-01,  4.7237e-01,   1.2445e+00,  2.8611e+00,  0.0000e+00,  0.0000e+00],0.2018,9
[-1.7200e+01,  2.8413e+01, -9.6788e-01,  6.7663e-01,  7.5484e-01,   1.8108e+00,  2.8296e+00, -1.4062e-01,  8.4375e-01],0.2018,8,  
[-1.4862e+01,  2.4362e+01, -1.0616e+00,  6.9811e-01,  7.6673e-01,   1.8108e+00, -2.9409e+00, -3.9062e-01,  6.5625e-01],0.2018,8, 
[-1.5562e+01,  2.5150e+01, -1.0304e+00,  6.8729e-01,  7.6673e-01,   1.8108e+00,  3.1416e+00, -3.1250e-01,  8.1250e-01],0.2018,8, 
[-1.4750e+01,  2.3625e+01, -1.0616e+00,  6.9811e-01,  7.6673e-01,   1.8108e+00, -2.7389e+00, -3.4375e-01,  4.6875e-01],0.2018,8, 
[-1.7175e+01,  2.3525e+01, -9.9913e-01,  6.7663e-01,  7.5484e-01,   1.8108e+00, -2.8820e+00, -2.0312e-01,  3.1250e-01],0.2018,8, 
[-1.7138e+01,  2.2850e+01, -9.9913e-01,  6.7663e-01,  7.5484e-01,   1.8108e+00, -2.7732e+00,  0.0000e+00,  2.6562e-01],0.2018,8, 
[-1.7188e+01,  2.7638e+01, -9.6788e-01,  6.7663e-01,  7.5484e-01,   1.8108e+00,  2.9078e+00, -1.5625e-01,  8.1250e-01],0.2018,8, 
[-1.6500e+01,  2.2813e+01, -1.0304e+00,  6.7663e-01,  7.5484e-01,   1.8108e+00, -2.6914e+00, -1.0938e-01,  2.9688e-01],0.2018,8,
[-1.4019e+01,  3.7244e+01, -8.9897e-01,  2.5536e+00,  6.5208e+00,   2.9854e+00,  2.7901e+00,  0.0000e+00,  0.0000e+00],0.1824,1, 
[-1.5525e+01,  2.4438e+01, -1.0616e+00,  6.8729e-01,  7.6673e-01,   1.8108e+00, -2.9912e+00, -3.1250e-01,  6.7188e-01],0.1824,8, 8, 8, 
[-1.4725e+01,  2.2888e+01, -1.0616e+00,  6.9811e-01,  7.6673e-01,   1.8108e+00, -2.4837e+00, -2.8125e-01,  2.8125e-01],0.1824,
[-1.7938e+01,  2.9150e+01, -9.3663e-01,  6.7663e-01,  7.5484e-01,   1.8108e+00,  2.8100e+00, -1.4062e-01,  7.6562e-01],0.1824,
[-9.8750e+00,  2.8475e+01, -1.4251e+00,  4.7237e-01,  4.8736e-01,   1.2252e+00,  2.9247e+00,  0.0000e+00,  0.0000e+00],0.1824,9, 
[ 3.2362e+01, -3.1575e+01, -2.4054e+00,  7.0911e-01,  7.5484e-01,   1.8108e+00,  2.1840e+00, -5.0000e-01,  5.9375e-01],0.1824,8, 8, 8, 8, 
[-1.6375e+01,  2.4400e+01, -1.0304e+00,  6.8729e-01,  7.6673e-01,   1.8108e+00, -2.9764e+00, -3.5938e-01,  5.6250e-01],0.1824,
[-1.7225e+01,  2.9138e+01, -9.3663e-01,  6.8729e-01,  7.5484e-01,   1.8108e+00,  2.7955e+00, -6.2500e-02,  8.1250e-01],0.1824,
[-1.4050e+01,  2.2825e+01, -1.1241e+00,  6.9811e-01,  7.5484e-01,   1.8108e+00, -2.2265e+00, -4.6875e-01,  7.8125e-02],0.1824,
[-1.0062e+01,  2.9938e+01, -1.4156e+00,  4.7237e-01,  4.7981e-01,   1.2062e+00,  2.8148e+00,  0.0000e+00,  0.0000e+00],0.1824,9, 
[-1.6450e+01,  2.3575e+01, -1.0304e+00,  6.8729e-01,  7.6673e-01,   1.8108e+00, -2.8431e+00, -2.9688e-01,  4.0625e-01],0.1824,8, 8, 8, 8, 8, 
[-1.6350e+01,  2.5188e+01, -1.0304e+00,  6.7663e-01,  7.5484e-01,   1.8108e+00, -3.1108e+00, -2.6562e-01,  6.7188e-01],0.1824,
[-1.6463e+01,  2.2087e+01, -1.0616e+00,  6.7663e-01,  7.4314e-01,   1.8108e+00, -2.5253e+00, -1.5625e-02,  1.5625e-01],0.1824,
[-1.7188e+01,  2.6837e+01, -9.9913e-01,  6.7663e-01,  7.5484e-01,   1.8108e+00,  3.0019e+00, -2.1875e-01,  7.5000e-01],0.1824,
[-1.7925e+01,  2.8450e+01, -9.3663e-01,  6.7663e-01,  7.5484e-01,   1.8108e+00,  2.8300e+00, -2.6562e-01,  7.3438e-01],0.1824,
[-4.8563e+00, -1.0769e+01, -2.2005e+00,  6.2578e-01,  2.0519e+00,   1.1510e+00, -2.9349e+00,  0.0000e+00,  0.0000e+00],0.1824,7, 
[-1.7162e+01,  2.4362e+01, -9.9913e-01,  6.7663e-01,  7.5484e-01,   1.8108e+00, -2.9590e+00, -3.5938e-01,  3.9062e-01],0.1824,8, 8
[-1.6388e+01,  2.5987e+01, -9.9913e-01,  6.7663e-01,  7.5484e-01,   1.8108e+00,  3.0509e+00, -2.0312e-01,  8.2812e-01],0.1824,

label: 0 score: 0.622459 bbox: -0.60625 -24.3687 -2.56163 2.08418 4.92217 1.81077 -3.08013 0.625 8
label: 7 score: 0.622459 bbox: -4.31875 -10.6125 -2.2005 0.666144 1.98874 1.15099 -2.93491 0 0
label: 3 score: 0.268941 bbox: -14.0938 37.2563 -1.00886 2.63465 8.11529 3.08022 2.76829 0 0
label: 8 score: 0.2227 bbox: 31.65 -30.7375 -2.43663 0.709106 0.75484 1.81077 2.20518 -0.640625 0.6875
label: 8 score: 0.201813 bbox: 30.9 -29.975 -2.46788 0.709106 0.75484 1.81077 2.20835 -0.671875 0.703125
label: 8 score: 0.201813 bbox: -17.1375 22.8375 -0.999133 0.687289 0.75484 1.81077 -2.83005 -0.03125 0.25
label: 8 score: 0.201813 bbox: -16.475 22.7875 -1.03038 0.687289 0.75484 1.81077 -2.69849 -0.1875 0.265625
label: 8 score: 0.201813 bbox: -17.2 23.525 -0.999133 0.687289 0.75484 1.81077 -2.90718 -0.171875 0.328125
label: 8 score: 0.201813 bbox: -14.7625 23.6375 -1.06163 0.698112 0.766727 1.81077 -2.73894 -0.328125 0.46875
label: 8 score: 0.201813 bbox: -15.5 24.4375 -1.03038 0.687289 0.766727 1.81077 -3.00606 -0.296875 0.703125
label: 8 score: 0.201813 bbox: -14.875 24.3625 -1.06163 0.698112 0.766727 1.81077 -2.94094 -0.375 0.703125
label: 8 score: 0.201813 bbox: -15.5625 25.1375 -1.03038 0.687289 0.766727 1.81077 3.11175 -0.296875 0.796875
label: 8 score: 0.201813 bbox: -17.1875 27.65 -0.967883 0.676634 0.75484 1.81077 2.88904 -0.1875 0.859375
label: 8 score: 0.201813 bbox: -17.2 28.3875 -0.936633 0.687289 0.75484 1.81077 2.82955 -0.09375 0.859375
label: 8 score: 0.201813 bbox: -17.225 29.1375 -0.905383 0.687289 0.75484 1.81077 2.80061 0.015625 0.875
label: 1 score: 0.182426 bbox: -14.625 38.7875 -0.836474 2.55359 6.52082 2.98545 2.8075 0 0
label: 7 score: 0.182426 bbox: -4.85625 -10.775 -2.20956 0.635639 2.02006 1.16912 -2.9381 0 0
label: 8 score: 0.182426 bbox: 32.425 -31.5125 -2.40538 0.709106 0.75484 1.81077 2.19628 -0.609375 0.609375
label: 8 score: 0.182426 bbox: -16.45 22.0875 -1.0476 0.676634 0.743137 1.78269 -2.53499 -0.046875 0.171875
label: 8 score: 0.182426 bbox: -14.0375 22.8375 -1.09288 0.698112 0.75484 1.81077 -2.2424 -0.484375 0.109375
label: 8 score: 0.182426 bbox: -16.425 23.5625 -0.999133 0.676634 0.766727 1.81077 -2.86527 -0.28125 0.390625
label: 8 score: 0.182426 bbox: -15.5375 23.65 -1.03038 0.687289 0.766727 1.81077 -2.8342 -0.265625 0.4375
label: 8 score: 0.182426 bbox: -16.35 25.2 -1.03038 0.676634 0.766727 1.81077 3.14159 -0.296875 0.6875
label: 8 score: 0.182426 bbox: -16.3875 25.975 -0.999133 0.676634 0.75484 1.81077 3.02097 -0.265625 0.84375
label: 8 score: 0.182426 bbox: -17.1875 26.8375 -0.967883 0.676634 0.75484 1.81077 2.9866 -0.234375 0.78125
label: 8 score: 0.182426 bbox: -16.45 26.8 -0.967883 0.687289 0.75484 1.81077 2.91479 -0.1875 0.921875
label: 8 score: 0.182426 bbox: -17.9125 28.425 -0.936633 0.676634 0.75484 1.81077 2.85465 -0.171875 0.75
label: 8 score: 0.182426 bbox: -17.95 29.175 -0.905383 0.676634 0.75484 1.81077 2.81004 -0.078125 0.828125
label: 8 score: 0.182426 bbox: -17.9625 29.95 -0.905383 0.687289 0.743137 1.81077 2.76109 -0.0625 0.875
label: 8 score: 0.182426 bbox: -19.575 31.6125 -0.857141 0.676634 0.743137 1.83928 2.81004 0.046875 0.84375
label: 8 score: 0.182426 bbox: -19.575 32.4 -0.842883 0.676634 0.743137 1.81077 2.80061 0.015625 0.9375
label: 9 score: 0.182426 bbox: -9.875 28.45 -1.43476 0.479805 0.487361 1.24452 2.91236 0 0


*/
